<template>
  <!-- Ops:按钮权限组件 -->
  <div :class="['role__container', { role__container_disabled: disabled }]">
    <!-- 遮罩层 -->
    <div class="role__container_mask" v-if="showMask" @click.stop="clickHandle"></div>
    <!-- 内容区 -->
    <template v-if="showContent">
      <slot></slot>
    </template>
    <!-- 默认自定义展示内容 -->
    <template v-else-if="!preview">
      <slot name="reserve"></slot>
    </template>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  name: 'RoleContainer',
  props: {
    roleKey: {
      type: String,
      default: ''
    },
    // 我权限是否展示
    preview: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {}
  },
  computed: {
    ...mapGetters(['abilityRoles']),
    hasRole() {
      if (!this.roleKey) return true
      return this.abilityRoles.some(item => {
        if (item.roleId === this.roleKey) return true
      })
    },
    disabled() {
      return this.roleKey && this.preview && !this.hasRole
    },
    showMask() {
      return !this.hasRole && this.preview
    },
    showContent() {
      return this.hasRole || this.preview
    }
  },
  mounted() {},
  methods: {
    clickHandle(e) {
      e.stopPropagation()
    }
  }
}
</script>

<style lang="less" scoped>
.role__container {
  display: inline-block;
  position: relative;
  cursor: pointer;

  .role__container_mask {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
  }
}
.role__container_disabled {
  filter: gray; /* IE6-9 */
  filter: grayscale(1); /* W3C */
  cursor: auto;
}
</style>
